<?php
namespace app\jxcshop\controller\sys_admin;
use app\AdminController;
use app\member\model\UsersModel;
use think\Db;

use app\distribution\model\DividendModel;

//*------------------------------------------------------ */
//-- 佣金明细
/*------------------------------------------------------ */
class Jiaquangudong extends AdminController{
	//*------------------------------------------------------ */
	//-- 初始化
	/*------------------------------------------------------ */
    public function initialize(){
   		parent::initialize();
        $this->Model = new \app\jxcshop\model\Jiaquangudong();
    }

	//*------------------------------------------------------ */
	//-- 首页
	/*------------------------------------------------------ */
    public function index(){

		$this->assign("start_date", date('Y/m/01',strtotime("-1 months")));
		$this->assign("end_date",date('Y/m/d'));
		$this->getList(true);
        return $this->fetch('index');
    }
	/*------------------------------------------------------ */
    //-- 获取列表
	//-- $runData boolean 是否返回模板
    /*------------------------------------------------------ */
    public function getList($runData = false,$is_cancel = false){

		$where = [];



        $data = $this->getPageList($this->Model, $where);

        //当天开始时间
        $start_time = strtotime(date("Y-m-d",time()));
        //当天结束之间
        $end_time = $start_time+60*60*10;

        foreach ($data['list'] as $k=>$v){
            $data['list'][$k]['username'] = $this->getUserfieldname($v['user_id'],'nick_name');
            $data['list'][$k]['rolename'] = $this->getUserrole($v['user_id']);
            $data['list'][$k]['mobile'] = $this->getUserfieldname($v['user_id'],'mobile');
            //$data['list'][$k]['zhituidailinum'] = $this->getUserzhituidailinum($v['user_id']);
            //$data['list'][$k]['tuanduidailinum'] = $this->getUserTuanduidailinum($v['user_id']);

            if(time() > $start_time && time() < $end_time){
           
                Db::table("jiaquangudong")->where("user_id","=",$v['user_id'])->update(['zhituidailinum' => $this->getUserzhituidailinum($v['user_id']),'tuanduidailinum' => $this->getUserTuanduidailinum($v['user_id'])]);
            }
        }

		$this->assign("data", $data);
		if ($runData == false){
			$data['content'] = $this->fetch('list')->getContent();
			unset($data['list']);
			return $this->success('','',$data);
		}
        return true;
    }

    /*
     * 会员身
     */
    public function getUserfieldname($user_id,$field = 'nick_name'){
        $userObj = Db::table("users")->field($field)->where("user_id","=",$user_id)->find();

        return $userObj[$field];
    }

    /*
     * 获取会员身份
     */
    public function getUserrole($user_id){
        $userObj = Db::table("users")->where("user_id","=",$user_id)->find();
        if($userObj['role_id'] == 0){
            return "粉丝";
        }else if($userObj['role_id'] == 1){
            return "代理";
        }else if($userObj['role_id'] == 2){
            return "经理";
        }else if($userObj['role_id'] == 3){
            return "会员";
        }
    }

    /*
     * 直推代理
     */
    public function getUserzhituidailinum($user_id){
        $pushCount = UsersModel::where(['pid' => $user_id])->field('user_id')->select()->toArray();
        if($pushCount && !empty($pushCount)){
            return count($pushCount);
        }else{
            return 0;
        }
    }


    /*
     * 获取团队下面几个代理
     *
     */
    public function getUserTuanduidailinum($uid){
        $usersArray = $this->getUnderlingUIds($uid);
        $wheres[] = ["role_id" , "=" , 1];
        $wheres[] = ["user_id" , "in" ,$usersArray];
        $userModel = new UsersModel();
        $isdaili = $userModel->where($wheres)->count();
        //echo Db('users','',false)->getLastSql();

        return $isdaili;
    }

    /**
     * 获取用户下属ID集合
     * @param $uid int
     * @param $ids string
     * @author linkai
     * @return string
     */
    public function getUnderlingUIds($uid, $ids='')
    {
        $userModel = new UsersModel();
        //$back = Db("users")->field('user_id')->where('pid',$uid)->select();
        $back = Db::table("users")->field('user_id')->where("pid = ".$uid)->select();  //user_id != 30683 and
        if (!empty($back) && is_array($back)) {
            foreach ($back as $v) {
                //防止当前人的ID重复去查询，形成恶性循环
                if ($v['user_id'] == $uid) {
                    continue;
                }
                //$back2 = Db("users")->where('pid',$v['user_id'])->count('user_id');
                //$back22 = $userModel->field('user_id')->where('pid',$v['user_id'])->select();
                $back22 = Db::table("users")->field('user_id')->where("pid = ".$v['user_id'])->select();
                if ($back22 > 0) {

                    $ids = $this->getUnderlingUIds($v['user_id'],$ids);
                    $ids .= ','.$v['user_id'];
                    foreach ($back22 as $vs) {
                        $ids .= ','.$vs['user_id'];
                    }
                } else {
                    $ids .= ','.$v['user_id'];
                }
            }
        }
        $ids = $uid.','.$ids.',';
        $ids = str_replace(',,', ",", $ids);
        $ids = trim($ids, ',');
        return $ids;
    }

}
